<template>

  <div>
    <el-form  :model="form" label-width="80px">
      <el-form-item label="部门编号">
        <el-input v-model="form.deptno"></el-input>
      </el-form-item>

      <el-form-item label="部门名称">
        <el-input v-model="form.dname"></el-input>
      </el-form-item>

      <el-form-item label="办公地址">
        <el-input v-model="form.loc"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save()">保存</el-button>

      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default{
  name:'DeptEdid',
  props:["deptno"],
  data(){
    return {
      form:{
        deptno:'',
        dname:'',
        loc:''
      }
    }
  },
  created() {
    if(this.deptno){
      this.getForm();
    }
  },
  methods:{
    //修改的时候初始化使用
    getForm(){
      // 获取远端图片
      this.$http({
        method:'get',
        url:'/api/beds?_type=queryById&deptno='+this.deptno
      })  .then((response) =>{
        this.form = response.data;
      }).catch((e)=>{
        console.log("失败的情况",e)
      });
    },
    save(){
      //form
      // http://127.0.0.1:8080/server/dept?_type=save&deptno=70&dname=Test&loc=TestLoc
      //     /dept?_type=save&deptno=70&dname=Test&loc=TestLoc
      // 获取远端图片

      //构造了 application/x-www-form-urlencoded
      const params = new URLSearchParams();
      params.append('deptno', this.form.deptno);
      params.append('dname', this.form.dname);
      params.append('loc', this.form.loc);

      let url = '/api/beds?_type=save';
      if(this.deptno){ //修改
        url =  '/api/beds?_type=update';
      }

      // form /
      this.$http({
        method:'post',
        url:url,
        params: params,
      })  .then((response) =>{
        let {success} = response.data;

        if(success){
          this.$message.success("保存成功");
          //关掉自己
          this.$emit("update:visible",false);
          this.$emit("close");
        }
      }).catch((e)=>{
        console.log("失败的情况",e)
      });

    }
  }
}


</script>

<style scoped>




</style>